package com.ty.springbootmybatisplusdruid;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.springbootmybatisplusdruid.pojo.User;
import com.ty.springbootmybatisplusdruid.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * UserMapperTester
 *
 * @aurhor Administrator  whs
 * @since 2024/9/3
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTester {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectById() { // 根据主键id查询
        User user = userMapper.selectById(88L);
        System.out.println("userName: " + user.getUsrName());
    }

    @Test
    public void testInsert(){ // 添加
        userMapper.insert(new User("Mybatis-Puls","123456",9L,1));
    }

    @Test
    public void testFindAll(){ // 查询所有
        List<User> userList = userMapper.selectList(null);
        for(User user : userList){
            System.out.println("usrName: "+user.getUsrName());
        }
    }

    @Test
    public void testUpdate(){ // 修改
        User user = new User();
        user.setUsrId(92l);
        user.setUsrName("离别情诗雨");
        user.setUsrPassword("w123");
        user.setUsrRoleId(3l);
        user.setUsrFlag(2);
        userMapper.updateById(user);
    }

    @Test
    public void testDelete(){ // 删除
        userMapper.deleteById(93L);
    }

    @Test
    public void testFind(){  // 条件查询 ： 根据用户名和密码查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("usr_name","离别情诗雨");
        wrapper.eq("usr_password","w123");
        List<User> userList = userMapper.selectList(wrapper);
        for(User user : userList){
            System.out.println("usrId: "+user.getUsrId());
            System.out.println("usrName: "+user.getUsrName());
        }
    }

    @Test
    public void testPagination(){ // 分页查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_role_id",2L); // 查询条件： 角色id为2

        // 分页对象： 查询获得User 对象，查看第1页数据 ， 每页显示2条记录
        Page<User> page = new Page<User>(1,2);
        IPage<User> userPage = userMapper.selectPage(page,wrapper); // 传入分页对象，条件
        System.out.println("总记录数 ---- --> " + userPage.getTotal());
        System.out.println("总页数 ---- --> " + userPage.getPages());
        System.out.println("当前页数 ---- --> " + userPage.getCurrent());
        System.out.println("每页记录数 ---- --> " + userPage.getSize());
        System.out.println("当前记录  ---- --> " );
        for (User user : userPage.getRecords() ){ // 获得查询记录
            System.out.println("usrId: "+user.getUsrId());
            System.out.println("usrName: "+user.getUsrName());
        }
    }








}
